package com.blockchain.user;

import com.blockchain.sql.DBConnect;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * user对象
 * @author luojiuchuan
 * @since 1.0
 * @version 1.0
 */
public class User {

    private String username;
    private String password;
    private String uemail;
    private String phone;
    private String uid;
    //此email是用户登录使用的,uemail是SQL获取得到的
    private String email;
    private JsonNode person;
    private String token;

    private ObjectNode objectMapper;

    public User(String email){
        this.email = email;
    }
    /**
     * 创建user对象
     * @author luojiuchuan
     * @since 1.0
     * @return Json {@code JsonNode}
     * */
    public JsonNode users() throws SQLException, ClassNotFoundException {
        //创建ObjectMapper对象用于将Java对象转换为JSON对象
        ObjectMapper objectMapper = new ObjectMapper();
        //创建JSON对象存储在obejectMapper的类容器
        ArrayNode jsonArray = objectMapper.createArrayNode();

        try {
            //在DBConnect类内已经存储了驱动类
            Connection conn = new DBConnect().getConnection();
            //预编译的SQL语句，?为参数位置
            PreparedStatement ps = conn.prepareStatement("select * from user where email = ?");
            ps.setString(1, email);
            //创建接收器，接收SQL结果
            ResultSet rs = ps.executeQuery();
            //读取SQL接收到的数据，如果没有则为null
            while (rs.next()) {
                //创建一个空JSON对象，ResultSet类不支持转换为JSON对象
                ObjectNode jsonNode = objectMapper.createObjectNode();
                //依次存储键值
                jsonNode.put("uid", rs.getString("uid"));
                jsonNode.put("name", rs.getString("uname"));
                jsonNode.put("phone", rs.getString("phone"));
                jsonNode.put("email", rs.getString("email"));
                jsonNode.put("password", rs.getString("upsw"));
                jsonNode.put("token", "");
                //将对象加入到容器中
                jsonArray.add(jsonNode);
                //将对象存储到user对象
                this.objectMapper = jsonNode;
            }
            //关闭SQL资源
            rs.close();
            ps.close();
            conn.close();
//            //将获取的资源释放到user对象中，由于容器内可以存在多个对象所以这里需要标明获取的目标对象
//            //依次读出JSON对象的键值
            this.username = this.objectMapper.get("name").asText();
            this.password = this.objectMapper.get("password").asText();
            this.uemail = this.objectMapper.get("email").asText();
            this.phone = this.objectMapper.get("phone").asText();
            this.uid = this.objectMapper.get("uid").asText();
            this.email = this.objectMapper.get("email").asText();
            this.person = this.objectMapper;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return person;
    }

    /**
     * 获取用户名
     * @author luojiuchuan
     * @since 1.0
     * @return String {@code phone}
     */
    public String getUsername() {
        return username;
    }
    /**
     * 获取电话号码
     * @author luojiuchuan
     * @since 1.0
     * @return String {@code phone}
     */
    public String getPhone() {
        return phone;
    }
    /**
     * 获取邮件地址
     * @author luojiuchuan
     * @since 1.0
     * @return String {@code email}
     */
    public String getEmail() {
        return uemail;
    }
    /**
     * 获取加密的密码
     * @author luojiuchuan
     * @since 1.0
     * @return String {@code password}
     */
    public String getPassword() {
        return password;
    }
    /**
     * 获取uid
     * @author luojiuchuan
     * @since 1.0
     * @return String {@code uid}
     */
    public String getUid() {
        return uid;
    }

    private ObjectNode getObjectMapper() {
        return objectMapper;
    }
    /**
     * 获取token
     * @author luojiuchuan
     * @since 1.0
     * @return String {@code token}
     */
    public String getToken() {
        return token;
    }

    /**
     * 后续添加token对象.
     * @param token {@code String token}
     * @author luojiuchuan
     * @since 1.0
     * @deprecated will at 1.1
     */
    public void addToken(String token) {
        getObjectMapper().put("token", token);
        this.token = token;
    }

    /**
     * 移除json对象内的键值，请谨慎使用
     * @author luojiuchuan
     * @param any value of {@code uid | uname | phone | email | password | token}
     * @since 1.0
     */
    public void dropThings(String any){
        getObjectMapper().remove(any);
    }
    /**
     * 添加任意的键值
     * @author luojiuchuan
     * @param key is a {@code String key}
     * @param value is a {@code String value}
     * */
    public void addThing(String key, String value){
        getObjectMapper().put(key, value);
    }
}
